package azalea.mqtt.message.builder;

import azalea.mqtt.message.builder.impl.PublishDataImpl;
import azalea.mqtt.message.utils.ByteUtils;

public class PublishMsgBuilder {
	public static byte[] publish(int  identifier,boolean dup,int qos,boolean retain,String topic,byte[] payload,int payloadLen) {
		byte[] buf=new byte[payload.length+topic.length()+2];
		buf[0]=(byte)(topic.length()/256);
		buf[1]=(byte)(topic.length()%256);
		ByteUtils.copy(topic.getBytes(),buf, 0,  2, topic.getBytes().length);
		ByteUtils.copy(payload,buf, 0,  2+topic.getBytes().length, payloadLen);
		return publish(identifier, dup, qos, retain, buf, payloadLen+topic.getBytes().length+2);
	}
	private static byte[] publish(int  identifier,boolean dup,int qos,boolean retain,byte[] payload,int payloadLen) {
		return publish((byte)((identifier>>8)&0xff), (byte)(identifier&0xff), dup, qos, retain, payload, payloadLen);
	}
	private static byte[] publish(byte msb,byte lsb,boolean dup,int qos,boolean retain,byte[] payload,int payloadLen) {
		return PublishDataImpl.publish( dup, qos, retain, msb,lsb, payload, payloadLen);
	}
	public static byte[] puback(int identifier) {
		return PublishDataImpl.puback((byte)((identifier>>8)&0xff), (byte)(identifier&0xff));
	}
	public static byte[] pubrec(int identifier) {
		return PublishDataImpl.puback((byte)((identifier>>8)&0xff), (byte)(identifier&0xff));
	}
	public static byte[] pubrel(int identifier) {
		return PublishDataImpl.puback((byte)((identifier>>8)&0xff), (byte)(identifier&0xff));
	}
	public static byte[] pubcomp(int identifier) {
		return PublishDataImpl.puback((byte)((identifier>>8)&0xff), (byte)(identifier&0xff));
	}
}
